/*
 * HoldLotAction.java
 *
 * Created on June 9, 2002, 1:23 AM
 */

package com.scalar.chiptrack.operations.process.actions;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.operations.process.forms.WipProcessFlowForm;
import com.scalar.chiptrack.users.User;

/**
 *
 * @author  Mallikarjun
 * @version 1.0
 */
public class WipProcessFlowAction extends ChipTrackAction{

        private static final String ACTION_NAME = "/wipProcessFlow.do";
        public ActionForward execute (ActionMapping mapping,
                                      ActionForm form,
                                      HttpServletRequest request,
                                      HttpServletResponse response)
                                      throws Exception {

            WipProcessFlowForm wipFlowForm = (WipProcessFlowForm)form;
            boolean success = false;
            HashMap errors = new HashMap();
            HttpSession session = request.getSession ();
            User user = ( User ) session.getAttribute ( SessionKeys.USER_KEY );
            if ( user == null )
            {
                session.setAttribute ( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString () );
                return ( mapping.findForward ( "login" ) );
            }

            String action = request.getParameter("action");
            if ( action == null || action.trim().length() == 0 )
            {
                action = "search";
            }
            if( "search".equalsIgnoreCase(action) )
            {
                request.setAttribute( "baseProducts", NewProductDAO.loadAllBaseProducts( null, true, true ) );
                request.setAttribute( "products", NewProductDAO.loadPartNumbers(null, 1, "FG", true, false, true, wipFlowForm.getBaseProductId() ));

                return mapping.findForward("show");
            }
            else if ( "PROCESS_FLOW".equalsIgnoreCase( action ) )
            {
                String productName = ProductDao.getBaseProductName( Integer.toString(wipFlowForm.getProductId()) );
                request.setAttribute("productId", Integer.toString(wipFlowForm.getProductId()));
                request.setAttribute("productName", productName);
                return mapping.findForward("view");
            }
            return null;
       }
}